import api from '../../api';
import { message } from 'antd';
export default {
  namespace: 'institutionConfigurationM',

  state: {
    data: [],
  },

  effects: {
    //查询机构信息
    *queryInstitution({ payload, callback }, { call, put }) {
      const response = yield call(api.institutionConfiguration.queryInstitution, payload);
      if (response) {
        if (response.code == 0) {
          callback('success', response.data);
        } else {
          callback('fail');
          message.error(`查询机构列表失败，失败原因：${response?.msg}`);
          console.error(`查询机构列表失败，失败原因：${response?.msg}`, '接口名称：InstructionTree 接口：portfolio-server/institution/InstructionTree');
        }
      } else {
        callback('fail');
        message.error(`查询机构列表失败，失败原因：${response?.msg}`);
        console.error(`查询机构列表失败，失败原因：${response?.msg}`, '接口名称：InstructionTree 接口：portfolio-server/institution/InstructionTree');
      }
    },
    //租户列表信息
    *getTenantList({ payload, callback }, { call, put }) {
      const response = yield call(api.institutionConfiguration.getTenantList, payload);
      if (response) {
        if (response.code == 0) {
          callback('success', response.data);
        } else {
          callback('fail');
          message.error(`查询租户列表失败，失败原因：${response?.msg}`);
          console.error(`查询租户列表失败，失败原因：${response?.msg}`, '接口名称：page-query 接口：portfolio-server/tenant/page-query');
        }
      } else {
        callback('fail');
        message.error(`查询租户列表失败，失败原因：${response?.msg}`);
        console.error(`查询租户列表失败，失败原因：${response?.msg}`, '接口名称：page-query 接口：portfolio-server/tenant/page-query');
      }
    },

    //删除接口
    *deleteInstitutionById({ payload, callback }, { call, put }) {
      const response = yield call(api.institutionConfiguration.deleteInstitutionById, payload);
      if (response && response.code === 0) {
        callback('success', response.data);
        message.success('删除成功');
      } else {
        message.error(`删除失败，失败原因：${response?.msg}`);
        console.error(`删除失败，失败原因：${response?.msg}`, '接口名称：删除的id名 接口：portfolio-server/institution/delete/${query.id}');
      }
    },
    // 获取负责人
    *getPrincipalList({ payload, callback }, { call, put }) {
      const response = yield call(api.getUserList, payload);
      if (response) {
        callback(response?.data);
      } else {
        message.error(`获取负责人数据失败，失败原因：${response?.msg}`);
        console.error(`获取负责人数据失败，失败原因：${response?.msg}`, '接口名称：findAll 接口：portfolio-server/user/findAll');
      }
    },
    // 新增机构
    *addInstitution({ payload, callback }, { call, put }) {
      const response = yield call(api.institutionConfiguration.addInstitution, payload);
      if (response && response.success) {
        callback(response);
        message.success('新增机构信息成功');
      } else {
        message.error(`新增机构信息失败，失败原因：${response?.mesg}`);
        console.error(`新增机构信息失败，失败原因：${response?.mesg}`, '接口名称：save 接口：portfolio-server/institution/save');
      }
    },

    // 修改机构
    *editInstitutionById({ payload, callback }, { call, put }) {
      const response = yield call(api.institutionConfiguration.editInstitutionById, payload);
      if (response && response.success) {
        callback(response);
        message.success('修改机构信息成功');
      } else {
        message.error(`修改机构信息失败，失败原因：${response?.mesg}`);
        console.error(`修改机构信息失败，失败原因：${response?.mesg}`, '接口名称：save 接口：portfolio-server/institution/save');
      }
    },

    //获取登录用户权限
    *getUser({ payload, callback }, { call, put }) {
      const response = yield call(api.user.queryCurrent, payload);
      if (response && response.success) {
        callback(response);
      } else {
        message.error(`获取登录用户权限失败，失败原因：${response?.mesg}`);
        console.error(`获取登录用户权限失败，失败原因：${response?.mesg}`, '接口名称：getUser 接口：user-server/usermanagement/authentication/getUser');
      }
    },

    //查看是否绑定用户
    *getCheckbindById({ payload, callback }, { call, put }) {
      const response = yield call(api.institutionConfiguration.getCheckbindById, payload);
      if (response) {
        callback(response);
      } else {
        message.error(`查看是否绑定用户失败，失败原因：${response?.mesg}`);
        console.error(`查看是否绑定用户失败，失败原因：${response?.mesg}`, '接口名称：getUser 接口：user-server/usermanagement/authentication/getUser');
      }
    },
    // 券池定义有使用到
    *Search({ payload }, { call, put }) {
      const data = yield call(api.institutionConfiguration.SearchCondition, payload);
      if (data && data.success) {
        yield put({
          type: 'save',
          payload: {
            data: data.data,
            loading: false,
          },
        });
      } else {
        message.error(`获取机构数据失败，失败原因：${data?.message}`);
        console.error(`获取机构数据失败，失败原因：${data?.message}`, '接口名称：condition 接口：portfolio-server/institution/condition');
      }
    },
    reducers: {
      save(state, { payload }) {
        return {
          ...state,
          ...payload,
        };
      },
    },
  },
};
